#import "ThumbsViewController.h"
#import "ThumbsViewControllerDelegate.h"
#import "ThumbsView.h"

@implementation ThumbsViewController

- (id)initWithDelegate:(id<ThumbsViewControllerDelegate>)_delegate
{
    if (self = [super init])
    {
        delegate = [_delegate retain];

        self.wantsFullScreenLayout = YES;
    }
    return self;
}

- (void)loadView
{
    thumbsView = [[ThumbsView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)
                  controller:self];
    self.view = thumbsView;
    [self reloadData];
}

- (void)viewDidUnload
{
    [thumbsView release];
    thumbsView = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication]
     setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault
     animated:NO];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}

- (void)dealloc
{
    [thumbsView release];
    [delegate release];
    [super dealloc];
}

- (void)reloadData
{
    int n = [delegate thumbsViewControllerPhotosCount:self];
    [thumbsView setNumberOfPhotos:n];
    for (int i = 0; i < n; ++i)
        [delegate thumbsViewController:self fetchPhotoAtIndex:i];
}

- (void)fetchedPhoto:(UIImage *)photo atIndex:(int)index
{
    [thumbsView setPhoto:photo atIndex:index];
}

- (void)thumbTapped:(int)index
{
    [delegate thumbsViewController:self selectedPhotoAtIndex:index];
}

@end
